package com.dushibao.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dushibao.admin.model.Admin;
import com.dushibao.admin.mv.DushibaoModelAndView;
import com.dushibao.admin.service.IAdminService;
import com.dushibao.admin.vo.request.AdminModelRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    IAdminService adminService;

    @RequestMapping("/admin/login")
    public ModelAndView login(){
        ModelAndView mv = new DushibaoModelAndView("/login/login");

        return mv;
    }

    @RequestMapping("/admin/doLogin")
    @ResponseBody
    public Boolean doLogin(HttpSession session,AdminModelRequest modelRequest,HttpServletResponse response){

        /**
         * 1、校验验证码
         *
         * 2、校验登录账号密码
         */
        Object srcCode = session.getAttribute("code");

        //如果session中没有code返回false
        //如果请求过来的code和session中的code不一致，也是返回fase
        //返回false代表没有校验通过
        if(srcCode == null || !srcCode.toString().equalsIgnoreCase(modelRequest.getCode())){
            return false;
        }

        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userName",modelRequest.getUserName());
        queryWrapper.eq("password",modelRequest.getPassword());

        Admin admin = this.adminService.getOne(queryWrapper);

        if(admin != null){

            //1、需要放到session中
            session.setAttribute("admin",admin);

            //2、放到cookie中
            if(modelRequest.getRememberMe()){
                Cookie adminUserName = new Cookie("adminUserName",modelRequest.getUserName());
                Cookie adminPassword = new Cookie("adminPassword",modelRequest.getPassword());

                //存到根目录下面
                adminUserName.setPath("/");
                adminPassword.setPath("/");

                //记住一个月
                adminUserName.setMaxAge(60 * 60 * 24 *30);
                adminPassword.setMaxAge(60 * 60 * 24 *30);

                response.addCookie(adminUserName);
                response.addCookie(adminPassword);

            }


            return true;
        }

        return false;
    }

    @RequestMapping("/admin/logOut")
    public String logOut(HttpSession session, HttpServletRequest request, HttpServletResponse response){

        session.removeAttribute("admin");

        Cookie adminUserName = new Cookie("adminUserName","");
        Cookie adminPassword = new Cookie("adminPassword","");

        //存到根目录下面
        adminUserName.setPath("/");
        adminPassword.setPath("/");

        //过期时间设置成0，立即失效
        adminUserName.setMaxAge(0);
        adminPassword.setMaxAge(0);

        response.addCookie(adminUserName);
        response.addCookie(adminPassword);

        return "redirect:/admin/login";
    }


}
